package Stream;


import java.util.*;
import java.util.stream.Stream;

//Stream:属于管道流，只能被使用一次，数据就会流转到下一个stream流中，stream流用完就会关闭。
public class demo1 {
    public static void main(String[] args) {
        //将单列集合转换为Stream流：
        List<String> list =new ArrayList<>();
        Stream<String> stream = list.stream();

        Set<String> set=new HashSet<String>();
        Stream<String> stream1 = set.stream();
        //将双列集合间接的转换为Stream流
        HashMap<String, Object> HashMap = new HashMap<>();
        //通过map集合获取key,存储到Set集合中,然后再转化为Stream流
        Set<String> strings = HashMap.keySet();
        Stream<String> stream2 = strings.stream();
        //通过map集合获取values,存储到Collection集合,中然后再转化为Stream流
        Collection<Object> values = HashMap.values();
        Stream<Object> stream3 = values.stream();

        //获取键值对
        Set<Map.Entry<String, Object>> entries = HashMap.entrySet();
        Stream<Map.Entry<String, Object>> stream4 = entries.stream();

        //数组转换为Stream流
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);//静态数组
        int[] arr={1,2,3,4,5,6};
        Stream<int[]> arr2 = Stream.of(arr);//动态数组
        System.out.println(arr2);
        String[] arr1={"小明","小刘","小军"};
        Stream<String> arr11 = Stream.of(arr1);
        System.out.println(arr11);

        one();
    }

    private static void one() {
        String[] arr1={"钟小明","小刘","小军"};
        Stream<String> arr11 = Stream.of(arr1);
       /* arr11.forEach((String name)->{
            System.out.println(name);
        });*/

        //优化后的lambda表达式
        arr11.forEach(name->System.out.println(name));
        /*public interface Stream<T> extends BaseStream<T, Stream<T>> {
            void forEach(Consumer<? super T> action);
            Consumer是消费型函数式接口
           public interface Consumer<T> {
               void accept(T t);
        */
        System.out.println("==========");
        Stream<String> arr12 = Stream.of(arr1);
        //使用filter方法进行过滤，返回的还是一个Stream流
        Stream<String> stream1 = arr12.filter((name) -> {
            return name.startsWith("小");
        });
        //对新生成的流进行遍历
        stream1.forEach((name)->{
            System.out.println(name);
        });
    }


}
